<?php

/**
 * Description of G_Gd
 *
 * @author Monk <eoe2005@gmail.com>
 */
if (function_exists('imagecreatetruecolor') === false)
{
    trigger_error('系统不支持GD库');
    die();
}

class G_Gd extends GC_Image
{

    public function clip($src, $w, $h, $outfile = null)
    {
        $im = $this->getImg($src);
        $_sw = imagesx($im);
        $_sh = imagesy($im);
        $f1 = $_sw/$_sh;
        $f2 = $w/$h;
        $x = 0;
        $y = 0;
        $_w = $_sw;
        $_h = $_sh;
        if($f1>$f2){
            $_h = $_sh;
            $_w = $_h*$f2;
            $x = ($_sw - $w)/2;
        }else{
            $_w = $_sw;
            $_h = $_w/$f2;
            $y = ($_sh - $_h)/2;
        }
        $out = imagecreatetruecolor($w, $h);
        imagecopyresampled($out,$im,0,0,$x,$y,$w,$h,$_w,$_h);
        if($outfile){
            $rs = imagejpeg($out,$outfile,100);
            imagedestroy($im);
            imagedestroy($out);
            return $rs;
        }else{
            header('Content-Type: image/jpeg');
            imagejpeg($out,$outfile,100);
            imagedestroy($im);
            imagedestroy($out);
        }
    }

    public function resize($src, $w, $h, $outfile = null)
    {
        $im = $this->getImg($src);
        $_sw = imagesx($im);
        $_sh = imagesy($im);
        $out = imagecreatetruecolor($w, $h);
        imagecopyresampled($out,$im,0,0,0,0,$w,$h,$_sw,$_sh);
        if($outfile){
            $rs = imagejpeg($out,$outfile,100);
            imagedestroy($im);
            imagedestroy($out);
            return $rs;
        }else{
            header('Content-Type: image/jpeg');
            imagejpeg($out,$outfile,100);
            imagedestroy($im);
            imagedestroy($out);
        }
    }

    public function scale($src, $w, $h, $outfile = null)
    {
        $im = $this->getImg($src);
        $_sw = imagesx($im);
        $_sh = imagesy($im);
        $_sf = $_sw/$_sh;
        $_of = $w/$h;
        $ow = $_sw;
        $oh = $_sh;
        if($_sf > $_of){
            if($_sh < $h){
                $oh = $h;
                $ow = $oh/$_sf;
            }
        }else{
            if($_sw < $w){
                $ow = $w;
                $oh = $ow*$_sf;
            }
        }
        $out = imagecreatetruecolor($ow, $oh);
        imagecopyresampled($out,$im,0,0,0,0,$ow,$oh,$_sw,$_sh);
        if($outfile){
            $rs = imagejpeg($out,$outfile,100);
            imagedestroy($im);
            imagedestroy($out);
            return $rs;
        }else{
            header('Content-Type: image/jpeg');
            imagejpeg($out,$outfile,100);
            imagedestroy($im);
            imagedestroy($out);
        }
    }

    public function showCode($code, $w, $h)
    {
        $im       = imagecreatetruecolor($w, $h);
        $bg       = imagecolorallocatealpha($im, 100, 100, 100, 100);
        $codelen  = count($code);
        imagefill($im, 0, 0, $bg); //设置背景了
        $fontw    = $w / $codelen; //一个字最多占用的宽度
        $fontsize = min($fontw, $h) / 2; //字体大小
        $fontArr  = array('font/SIMYOU.TTF', 'font/STFANGSO.TTF', 'font/STHUPO.TTF', 'font/STXINGKA.TTF', 'font/simfang.ttf');
        $fontlen  = count($fontArr) - 1;
        $index    = 0;
        foreach ($code AS $val)
        {
            $color    = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
            $fi       = rand(0, $fontlen);
            $fontfile = $fontArr[$fi];
            $angle    = rand(-30, 30);
            //var_dump($fi,$fontfile);
            if (function_exists('imagettftext'))
            {
                $position = imageftbbox($fontsize, $angle, $fontfile, $val);
                $_x       = $fontw * $index + abs(min($position[0], $position[2], $position[4], $position[6]));
                //$_y       = max($position[1], $position[3], $position[5], $position[7]) - min($position[1], $position[3], $position[5], $position[7]);
                $_y       = ($h / 2) + abs(min($position[1], $position[3], $position[5], $position[7]) / 2);
                imagefttext($im, $fontsize, $angle, $_x, $_y, $color, $fontfile, $val);
            } elseif (function_exists('imagepstext'))
            {
                $fontindex = imagepsloadfont($fontfile);
                $position  = imagepstext($im, $val, $fontindex, $fontsize, $color, $bg, $x, $y, 0, rand(0, 10));
                $x         = $position[2];
                $y         = abs($h - min($h, $position[1])) / 2;
            } else
            {
                trigger_error('系统不支持字体');
                die();
            }
            $index++;
        }
        header("Content-type: image/png");
        imagepng($im);
        imagedestroy($im);
    }

    public function watermark($src, $waterfile, $position = G_IMAGE_WATERMARK_BOTTOM_RIGHT, $outfile = null)
    {
        $im = $this->getImg($src);
        $_sw = imagesx($im);
        $_sh = imagesy($im);
        $wm = $this->getImg($waterfile);
        $_ww = imagesx($wm);
        $_wh = imagesy($vm);
        $_ow = $_ww;
        $_oh = $_wh;
        if($_ww > $_sw){
            $_ow = $_sw / 2;
            if($_wh > $_sh){
                $_oh = $_sh/2;
            }
        }else{
            if($_wh > $_sh){
                $_oh = $_sh/2;
                $_ow = $_ww /($_wh/$_oh);
            }
        }
        $_x = 0;
        $_y = 0;
        switch ($position){
            case G_IMAGE_WATERMARK_TOP_LEFT:
                $_x = $_sw - $_ow;
                break;
            case G_IMAGE_WATERMARK_TOP_MIDDLE:
                $_x = ($_sw - $_ow)/2;
                break;
            case G_IMAGE_WATERMARK_BOTTOM_RIGHT:
                $_y = $_sh - $_oh;
                break;
            case G_IMAGE_WATERMARK_BOTTOM_LEFT:
                $_x = $_sw - $_ow;
                $_y = $_sh - $_oh;
                break;
            case G_IMAGE_WATERMARK_BOTTOM_MIDDLE:
                $_y = $_sh - $_oh;
                $_x = ($_sw - $_ow)/2;
                break;
            default :
                break;
        }
        imagecopyresampled($im,$wm,$_x,$_y,0,0,$_ow,$_oh,$_ww,$_wh);
        if($outfile){
            $rs = imagejpeg($out,$outfile,100);
            imagedestroy($im);
            imagedestroy($out);
            return $rs;
        }else{
            header('Content-Type: image/jpeg');
            imagejpeg($out,$outfile,100);
            imagedestroy($im);
            imagedestroy($out);
        }
    }

    private function getImg($src)
    {
        $size = getimagesize($src);
        switch ($size[2])
        {
            case 1:
                return imagecreatefromgif($src);
            case 2:
                return imagecreatefromjpeg($src);
            case 3:
                return imagecreatefrompng($src);
        }
        return false;
    }

    /* private function imagefilledellipse($im,$w,$h,$f,$t){

      } */
}
